home *** CD-ROM | disk | FTP | other *** search
- Path: news.th-darmstadt.de!news
- From: Enno Sandner <enno@intellektik.informatik.th-darmstadt.de>
- Newsgroups: comp.lang.c++
- Subject: Re: another overloading question
- Date: Thu, 04 Apr 1996 08:58:03 +0200
- Organization: Fachbereich Informatik, TH Darmstadt
- Message-ID: <316372FB.167EB0E7@intellektik.informatik.th-darmstadt.de>
- References: <4ju98d$3al@panoramix.fi.upm.es>
- NNTP-Posting-Host: kitz.intellektik.informatik.th-darmstadt.de
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; SunOS 4.1.3 sun4m)
-
- Sacha wrote:
- >
- > Hi,
- >
- > Can I overload = for simple types?
- >
- > I mean, if for example I have:
- >
- > class Colour
- > {
- > private:
- > float r_, g_, b_;
- >
- > public:
- > Colour(float r, float g, float b)
- > {r_=r; g_=g; b_=b;}
- > };
- >
- > is there some way I can overload = so that I can do things like:
- >
- > Colour colour(0.99238, 0.172364, 0.273);
- >
- > int intensity = colour;
- >
- > such that intensity is assigned a value computed from r,g,b (e.g. the average)?
- >
- > I think someone else may have asked this, phrasing it all with lvalues and
- > rvalues, but I lost the thread.
- >
- > I suspect that the compiler treats simple types at compile time and there may be
- > no way to do this. I suppose I can substitute it with a normal member function...
- >
-
- You may provide a conversion operator that enables objects
- of class Colour to represent themselfs as integers, i.e.
-
- class Colour {
- ...
- public:
- ...
- operator int () const { return the_int_rep; }
- ...
- };
-
- An expression like 'int intensity=colour' will invoke
- the above conversion operator.
-
- Enno
-